package com.yc.yc112plyqnzf.controller;


import com.yc.yc112plyqnzf.entity.House;
import com.yc.yc112plyqnzf.mapper.HouseMapper;
import com.yc.yc112plyqnzf.service.IHouseService;
import com.yc.yc112plyqnzf.vo.Result;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

/**
 * <p>
 * 青鸟租房-房屋表-2 前端控制器
 * </p>
 *
 * @author yc
 * @since 2022-09-03
 */
@RestController
@RequestMapping("/yc112plyqnzf/house")
public class HouseController {


    @Resource
    private HouseMapper houseMapper;

    @Resource
    private IHouseService houseService;

    @RequestMapping("query")
    public List<House> query(){
        return houseMapper.selectList(null);
    }

    @RequestMapping("save")
    public Result save(@Valid House house, Errors errors){
        if(errors.hasErrors()){
            return  new Result(0,"字段验证错误",errors.getFieldErrors());
        }
        houseService.saveOrUpdate(house);
        return new Result(1,"房屋添加成功",null);
    }

    @RequestMapping("queryById")
    public House queryById(int id){
        return houseMapper.selectById(id);
    }

    @RequestMapping("deleteById")
    public Result deleteById(int id){
        try{
            houseMapper.deleteById(id);
            return new Result(1,"删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(0,"删除失败",null);
        }
    }


}

